﻿using System.Collections;
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

[Serializable]
public class GT_AddNode : GT_NodeBase
{
    #region Veriables
    public float nodeSum;
    public GT_NodeOutput output;
    public GT_NodeInput inputA;
    public GT_NodeInput inputB;
    #endregion

    #region Constructor
    public GT_AddNode()
    {
        output = new GT_NodeOutput();
        inputA = new GT_NodeInput();
        inputB = new GT_NodeInput();
    }
    #endregion

    #region Main Methods
    public override void InitNode()
    {
        base.InitNode();
        nodeType = NodeType.Add;
        nodeRect = new Rect(10f, 10f, 200f, 65f);
    }

    public override void UpdateNode(Event e, Rect viewRect)
    {
        base.UpdateNode(e, viewRect);
    }

    #if UNITY_EDITOR
    public override void UpdateNodeGUI(Event e, Rect viewRect, GUISkin viewSkin)
    {
        base.UpdateNodeGUI(e, viewRect, viewSkin);
        if (GUI.Button(new Rect(nodeRect.x + nodeRect.width, nodeRect.y + nodeRect.height * 0.5f - 12f, 24f, 24f), "", viewSkin.GetStyle("NodeOutput")))
        {
            if (parentGraph != null)
            {
                parentGraph.wantsConnection = true;
                parentGraph.connectionNode = this; 
            }
        }

        if (GUI.Button(new Rect(nodeRect.x - 24f, nodeRect.y + nodeRect.height * 0.33f - 14f, 24f, 24f), "", viewSkin.GetStyle("NodeInput")))
        {
            if (parentGraph != null)
            {
                inputA.inputNode = parentGraph.connectionNode;
                inputA.isOccupied = inputA.inputNode != null ? true : false;

                parentGraph.wantsConnection = false;
                parentGraph.connectionNode = null;
            }
        }

        if (GUI.Button(new Rect(nodeRect.x - 24f, nodeRect.y + nodeRect.height * 0.33f * 2f - 8f, 24f, 24f), "", viewSkin.GetStyle("NodeInput")))
        {
            if (parentGraph != null)
            {
                inputB.inputNode = parentGraph.connectionNode;
                inputB.isOccupied = inputB.inputNode != null ? true : false;

                parentGraph.wantsConnection = false;
                parentGraph.connectionNode = null;
            }
        }


        nodeSum = 0.0f;
        if(inputA.isOccupied && inputB.isOccupied)
        {
            GT_FloatNode nodeA = (GT_FloatNode)inputA.inputNode;
            GT_FloatNode nodeB = (GT_FloatNode)inputB.inputNode;

            if(nodeA != null && nodeB != null)
            {
                nodeSum = nodeA.nodeValue + nodeB.nodeValue;
            } else
            {

            }

        }

        DrawInputLines();
    }

    public override void DrawNodeProperties()
    {
        base.DrawNodeProperties();
        EditorGUILayout.FloatField("Sum: ", nodeSum);

    }

    #endif
    #endregion

    #region Utility methods
    void DrawInputLines()
    {
        if (inputA.isOccupied && inputA.inputNode != null)
        {
            Debug.Log("Drawing line");
            DrawLine(inputA, 1f); 
        }
        else
        {
            inputA.isOccupied = false;
        }

        if (inputB.isOccupied && inputB.inputNode != null)
        {
            DrawLine(inputB, 2f); 
        } else
        {
            inputB.isOccupied = false;
        }
    }

    private void DrawLine(GT_NodeInput curInput, float inputID)
    {
        Handles.BeginGUI();

        Handles.color = Color.white;
        Handles.DrawLine(new Vector3(curInput.inputNode.nodeRect.x + curInput.inputNode.nodeRect.width + 24f,
                                    curInput.inputNode.nodeRect.y + curInput.inputNode.nodeRect.height * 0.5f, 0f),
                        new Vector3(nodeRect.x - 24f, nodeRect.y + nodeRect.height * 0.33f * inputID, 0f));
        Handles.EndGUI();
    }
    #endregion



}
